package com.rc.saas.tenant.common.utils;


import java.math.BigDecimal;


public class DataUtil {

    /**
     * 数据项编码规则：第1个数据编码，第2个数据长度，第3个数据类型，第4个倍率（第1，2个是必须有的）
     */
    public final static Object[][] dataCodes =
            {
                    {"16 80", 2, String.class}/** 获取地区码 */, {"1F 89", 4, String.class},
                    {"1F B6", 6, BigDecimal.class},/**手动停止*/{"04 C9", 2, BigDecimal.class, 2},/**soc*/
                    {"30 80", 6, String.class},/**获取终端时间*/{"2F B6", 9, BigDecimal.class},/**三相电流*/
                    {"1F 90", 2, BigDecimal.class, 1}, {"88 C8", 3, BigDecimal.class, 3},/**电压*/
                    {"89 C8", 3, BigDecimal.class, 3}/**电流*/, {"11 B6", 3, BigDecimal.class, 2},/**a相电压*/
                    {"21 B6", 3, BigDecimal.class, 2},/**a相电流*/{"82 C8", 3, BigDecimal.class, 3},
                    {"8D C8", 4, BigDecimal.class, 2},/**所充电量*/{"8E C8", 4, BigDecimal.class, 2}/**总金额*/,
                    {"40 C8", 2, String.class}/**电枪状态码*/, {"21 C4", 3, BigDecimal.class, 4}/**电费率1*/,
                    {"22 C4", 3, BigDecimal.class, 4}/**电费率2*/, {"23 C4", 3, BigDecimal.class, 4}/**电费率3*/,
                    {"24 C4", 3, BigDecimal.class, 4}/**电费率4*/, {"31 C8", 8, String.class}/**读取当前交易卡号*/,
                    {"28 C4", 3, String.class}/**桩的管理员密码*/, {"EC EL", 1, String.class}/**自定义充电结果bcd码*/,
                    {"41 D1", 1, String.class}/**上报数据的时间*/, {"8E D8", 4, BigDecimal.class, 2},/**基本电费*/
                    {"51 B6", 1, String.class}/**电价时间段的ID*/, {"60 D8", 4, BigDecimal.class, 2}/**插枪充电返回码*/,
                    {"8E E9", 4, BigDecimal.class, 2}/**服务费用**/
            };


    /**
     * 任务号
     */
    public static String[] taskNums = {};
    public static final String SPLIT = " ";

    /**
     * 权限等级00为低，11为高
     */
    public final static String[] PERMISSON = {"00", "11"};

    /**
     * 构造存放当前数据到redis时的key 终端地址 ，枪号，数据项目编码,用户订单号
     *
     * @param rtua
     * @param gunNum
     * @param code
     * @return
     */
    public static String getCurrDataKey(String rtua, String gunNum, String code, String orderNum) {
        StringBuffer key = new StringBuffer("current_data:");
        key.append(rtua).append(":");
        if (gunNum.length() < 2)
            key.append("0"); // 不足两位补0
        key.append(gunNum).append(":").append(code);
        if (orderNum != null) {
            key.append(":").append(orderNum);
        }

        return key.toString();
    }

    /**
     * 构造存放当前数据到redis时的key 终端地址 ，枪号，数据项目编码
     * (参考ReadAction.hearBeat的key构造方法)
     *
     * @param rtua
     * @param gunNum
     * @param code
     * @return
     */
    public static String getCurrDataKey(String rtua, String gunNum, String code) {
        StringBuffer key = new StringBuffer("current_data:");
        key.append(rtua).append(":");
        if (gunNum.length() < 2)
            key.append("0"); // 不足两位补0
        key.append(gunNum).append(":").append(code);

        //if(cardNo!=null){
        //	key.append(":").append(cardNo);
        //}

        return key.toString();
    }

    public static String getKey(String... keys) {
        StringBuffer key = new StringBuffer();
        for (String str : keys) {
            if (str == null) {
                continue;
            }
            if (str.length() == 1) {
                str = "0" + str;
            }

            key.append(str).append(SPLIT);
        }
        return key.toString().trim();
    }


    /**
     * 验证请求的数据项目编码 并进行低位在前高位在后的转换
     *
     * @param code C904 -> 04 C9
     * @return
     */
    public static String[] turnCode(String... code) {
        if (code.length == 0) {
            return null;
        }
        String[] codes = new String[code.length];
        boolean flag = false;
        for (int i = 0; i < code.length; i++) {
            if (code[i].length() != 4) {
                return null;
            }
            code[i] = code[i].toUpperCase();
            codes[i] = code[i].substring(2) + SPLIT + code[i].substring(0, 2);
            // 验证是否存在
            flag = false;
            for (Object[] objs : dataCodes) {
                if (objs[0].equals(codes[i]))
                    flag = true;
            }
            if (!flag) {
                return null;
            }
        }

        return codes;
    }

    /**
     * 获取数据项编码对应的数据类型
     *
     * @param code
     * @return
     */
    public static Class getValueType(String code) {
        for (Object[] objs : dataCodes) {
            if (objs[0].equals(code)) {
                return (Class) objs[2];

            }
        }
        return null;
    }

    /**
     * 获取倍率
     *
     * @param code
     * @return
     */
    public static int getValueRate(String code) {
        for (Object[] objs : dataCodes) {
            if (objs[0].equals(code)) {
                return (int) objs[3];
            }
        }
        return 0;
    }

    /**
     * 将数据反转
     *
     * @param value
     * @return
     */
    public static String turnValue(String value) {
        String[] strs = value.split(SPLIT);
        StringBuffer sb = new StringBuffer();
        for (int i = strs.length - 1; i >= 0; i--) {
            sb.append(strs[i]);
        }
        return sb.toString();
    }

    /**
     * 根据倍率将数据转成实际数据
     *
     * @param value
     * @param rate
     * @param isHex 是否转为10进制
     * @return
     */
    public static BigDecimal getBDValue(String value, int rate, boolean isHex) {
        value = DataUtil.turnValue(value);
        BigDecimal val = null;
        if (isHex) {
            int v = Integer.parseInt(value, 16);
            val = new BigDecimal(v);
        } else {
            val = new BigDecimal(value);
        }
        val = val.multiply(new BigDecimal(Math.pow(10, rate * -1)));
        return val.setScale(rate, BigDecimal.ROUND_DOWN);
    }


    /**
     * 根据倍率将数据转成实际数据
     *
     * @param value
     * @param rate
     * @param isHex 是否转为16进制
     * @return
     */
    public static BigDecimal getZcBDValue(String value, int rate, boolean isHex) {
        //value = DataUtil.turnValue(value);
        String replace = value.replace(DataUtil.SPLIT, "");
        BigDecimal val = null;
        if (isHex) {
            int v = Integer.parseInt(replace, 16);
            val = new BigDecimal(v);
        } else {
            val = new BigDecimal(replace);
        }

        if (BigDecimal.ZERO.compareTo(val) == 0) {
            return BigDecimal.ZERO;
        }

        val = val.multiply(new BigDecimal(Math.pow(10, rate * -1)));
        return val.setScale(rate, BigDecimal.ROUND_DOWN);
    }

    public static String coverPosition(String str) {
        if (str.length() < 2) {
            return "0" + str;
        }
        return str;
    }


    /**
     * 锐速 价格转换
     *
     * @return
     */

    public static String getRsValutToFee(BigDecimal value) {
        int valuePower = value.multiply(new BigDecimal(Math.pow(10L, 2))).intValue();
        String format = Integer.toHexString(valuePower).toUpperCase();
        //补0
        //format = String.format("%06d", format);
        format = addZeroForNum(format, 8);

        String valueReport = "";
        for (int i = 4; i > 0; i--) {
            valueReport = valueReport + SPLIT + format.substring(i * 2 - 2, i * 2);
            ;
        }
        return valueReport.trim().substring(0, 3 * 3 - 1);
    }

    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);// 左补0
                // sb.append(str).append("0");//右补0
                str = sb.toString();
                strLen = str.length();
            }
        }

        return str;
    }


    /**
     * 对发送充电指令做唯一性校验，不能同时发送充电指令.
     *
     * @param rtua
     * @param gunNum
     * @return
     */
    public static String getCheckPileTime(String rtua, String gunNum) {
        StringBuffer key = new StringBuffer("pileCheck:time");
        key.append(rtua).append(":");
        if (gunNum.length() < 2)
            key.append("0"); // 不足两位补0
        key.append(gunNum).append(":").append(gunNum);
        return key.toString();
    }


    public static String getChannelIdKey(String channelAsLongText) {
        StringBuffer key = new StringBuffer("channelId");
        key.append(":").append(channelAsLongText);
        return key.toString();
    }


    public static void binaryToDecimal(int n) {
        int t = 0;  //用来记录位数
        int bin = 0; //用来记录最后的二进制数
        int r = 0;  //用来存储余数
        while (n != 0) {
            r = n % 2;
            n = n / 2;
            bin = (int) (r * Math.pow(10l, t));
            t++;
        }
        System.out.println(bin);
    }

    public static void main(String[] args) {
        //BCD数据转实际数据;
        //System.out.println(DataUtil.getBDValue("50 05 00 00 ", 2, false));

        //BigDecimal bigDecimal = new BigDecimal(1);
        //String valuetoHex = getValuetoHex(bigDecimal, 5);
        //System.out.println(valuetoHex);
        //
        //BigDecimal zcBDValue = getBDValue("B8 05 FF FF", 5, true);
        //System.out.println(zcBDValue);

        //System.out.println(getBCDTime());

        //String valuetoHex = DataUtil.toHexFrameByAccount(new BigDecimal(10000), 0);
        //System.out.println(valuetoHex);
        //
        //
        //double pow = Math.pow(10l, 0);
        //System.out.println(pow);
        //
        //BigDecimal bdValue = DataUtil.getBDValue("40 42 0F 00", 0, true);
        //System.out.println(bdValue);

        //int valuePower = new BigDecimal("10.0008").multiply(new BigDecimal(Math.pow(10L, 3))).intValue();
        System.out.println(getRsValutToFee(new BigDecimal("1")));
        //BigDecimal dcffffff = getBDValue("DCFFFFFF", 0, true);
        long v = Long.parseLong("E504820C", 16);
        System.out.println(v);
    }


}
